package com.quo.controller;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletOutputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.quo.entity.Email;
import com.quo.service.EmailService;
import com.quo.utils.Result;
import com.quo.utils.ResultCode;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

@RestController("emailController")
@RequestMapping(value="/api")
public class EmailController {
	@Value("${spring.mail.username}")
	private String from;
	@Autowired
	private JavaMailSender javaMailsender;
	@Autowired
	private EmailService eService;
	
	@RequestMapping(value = "/quote-email", method = RequestMethod.POST)
	public Result send(@RequestBody Email email)  {
		MimeMessage mineMessage = javaMailsender.createMimeMessage();
		Long qid = email.getQid();
		Email emailBySql = eService.getEmailByQid(qid);
		email.setEmail(emailBySql.getEmail());

		try {
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mineMessage, true);
			mimeMessageHelper.setTo(email.getEmail());
			mimeMessageHelper.setSubject(email.getSubject());
			mimeMessageHelper.setText(email.getContext());
			mimeMessageHelper.setFrom(from);

			System.out.println("aaa");
			// 1.引入jasper文件
			// Resource resource = new ClassPathResource("jasper/order2.jasper");
			InputStream fis = getClass().getResourceAsStream("/jasper/quote.jasper");

			// FileInputStream fis = new FileInputStream(resource.getFile());

			// 2.创建JasperPrint,向jasper文件中填充数据
			ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();

			try {
				Map parameters = new HashMap<>();
				parameters.put("qid", qid);

				Connection conn = getConnection();
				
				JasperPrint print = JasperFillManager.fillReport(fis, parameters, conn);
				JasperExportManager.exportReportToPdfStream(print, pdfOut);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					 pdfOut.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
			InputStream pdf = new ByteArrayInputStream(pdfOut.toByteArray());

            try {
				mimeMessageHelper.addAttachment("見積書.pdf", new ByteArrayResource(IOUtils.toByteArray(pdf)));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            javaMailsender.send(mineMessage);
            return new Result(ResultCode.SUCCESS);
		} catch (MessagingException e) {
			return new Result(ResultCode.FAIL);
		}

	}

	public Connection getConnection() throws Exception {
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@82.157.249.88:1521/xe", "tofu15",
				"LxUaqVk5bNobMGLyDwnY");
		return conn;
	}
}
